<template>
  <view>
    <!-- <my-search :bgColor="'#000000'" :radius="5"></my-search> -->
    <my-search @gotoSearch="gotoSearch"></my-search>
    <view class="scroll-view-box">
      <!-- 左侧 -->
      <scroll-view class="left-scrollr-view" scroll-y="true" :style="{height: windowHeight + 'px'}">
        <view :class="['left-scrollr-view-item', index === activeIndex ? 'active' : '']" v-for="(item, index) in cateList" :key="item.cat_id" @click="activeChange(index)">{{item.cat_name}}</view>
      </scroll-view>
      <!-- 右侧 -->
      <scroll-view class="right-scrollr-view" scroll-y="true" :style="{height: windowHeight + 'px'}" :scroll-top="scrollTop">
        <!-- 二级分类 -->
        <view class="cate-lv2" v-for="(item2, index2) in cateLevel2" :key="index2" v-if="item2.children">
          <view class="cate-lv2-title">{{item2.cat_name}}</view>
          <!-- 三级分类 -->
          <view class="cate-lv3-list">
            <view class="cate-lv3-item" v-for="(item3, index3) in item2.children" :key="index3" @click="gotoGoodsList(item3)">
              <image :src="item3.cat_icon" mode=""></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
            
          
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  // 导入自己封装的 mixin 模块
  import badgeMix from '@/mixins/tabbar-badge.js'
  
  export default {
    mixins: [badgeMix],
    data() {
      return {
        windowHeight: 0, // 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
        cateList: [],
        activeIndex: 0,
        cateLevel2: [],
        // 滚动条距离顶部的距离
        scrollTop: 0
      };
    },
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      // 为窗口可用高度动态赋值
      this.windowHeight = sysInfo.windowHeight - 50 // 减去搜索栏的高度
      
      this.getCateList()
    },
    methods: {
      // 获取分类数据
      async getCateList() {
        const {data: res} = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$toast()
        this.cateList = res.message
        this.cateLevel2 = this.cateList[0].children
      },
      activeChange(index) {
        this.activeIndex = index
        this.cateLevel2 = this.cateList[index].children
        // 切换一级分类后重置滚动条的位置
        this.scrollTop = this.scrollTop ? 0 : 1
      },
      gotoGoodsList(item3) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item3.cat_id
        })
      },
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-box {
    display: flex;
    .left-scrollr-view {
      width: 120px;
      .left-scrollr-view-item {
        background-color: #F7F7F7;
        line-height: 60px;
        text-align: center;
        font-size: 14px;
        
        &.active {
          background-color: #FFFFFF;
          position: relative;
          // 伪类添加竖线
          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #C00000;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
          }
        }
      }
    }
    
    .right-scrollr-view {
      .cate-lv2 {
        .cate-lv2-title {
          font-size: 16px;
          font-weight: bold;
          text-align: center;
          padding: 15px 0;
        }
        .cate-lv3-list{
          display: flex;
          flex-wrap: wrap;
          .cate-lv3-item {
            width: 33.33%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            margin-bottom: 10rpx;
            image {
              width: 65px;
              height: 65px;
            }
            text {
              font-size: 12px;
            }
          }
    
        }
      }
    }
  }
</style>
